package com.oa.controller;

import com.oa.vo.Message;
import com.oa.vo.Page;
import com.oa.vo.Result;
import com.oa.pojo.Department;
import com.oa.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@CrossOrigin
@ResponseBody
@Controller
public class DepartmentConntroller {

    @Autowired
    private DepartmentService departmentService;

    @RequestMapping(value = "/getDepartmentPage")
    public Result getDepartmentPage(Integer pageNo, Integer pageSize){
        if(pageNo == null){
            pageNo=1;
        }
        if(pageSize == null){
            pageSize=3;
        }
        Page page = departmentService.getDepartmentPage(pageNo,pageSize);
        Message message = new Message();
        Result result = new Result();
        result.setData(page);
        result.setMessage(message);
        return result;
    }

    @RequestMapping(value = "/getDepartment")
    public Result getDepartment(Integer deptId){
        Department department = departmentService.getDepartment(deptId);
        Message message = new Message();
        Result result = new Result();
        result.setData(department);
        result.setMessage(message);
        return result;
    }

    @RequestMapping(value = "/getDepartmentList")
    public Result getDepartmentList(){
        List<Department> departmentList = departmentService.getDepartmentList();
        Message message = new Message();
        Result result = new Result();
        result.setData(departmentList);
        result.setMessage(message);
        return result;
    }

    @RequestMapping(value = "/addDepartment")
    public Result addDepartment(@RequestBody Department department){
        System.out.println(department);
        Date date = new Date();
        department.setCreateTime(date);
        departmentService.addDepartment(department);
        Result result = new Result();
        return result;
    }

    @RequestMapping(value = "/delDepartment")
    public Result delDepartment(Integer departmentId){
        Message message = new Message();
        Result result = new Result();
        if(departmentId == null){
            message.setCode(200);
            message.setMsg("No ID");
            result.setMessage(message);
            return result;
        }
        message.setCode(100);
        message.setMsg("ok");
        departmentService.delDepartment(departmentId);
        result.setMessage(message);
        return result;
    }

    @RequestMapping(value = "/updateDepartment")
    public Result updateDepartment(@RequestBody Department department){
        Date date = new Date();
        department.setCreateTime(date);
        departmentService.updateDepartment(department);
        Result result = new Result();
        return result;
    }

}
